package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.maps.model.LatLng;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGetPositionBinding;
import com.sgb.kjwl.model.entity.supplier3.SupplierEntity3;
import com.sgb.kjwl.view.widget.map.GetMapInfoPop;
import com.sgb.kjwl.view.widget.pick.PickMapAppWindow;
import com.swgk.core.base.model.entity.RouterKey;
import com.sgb.link.router.PathUser;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.List;

import kotlin.jvm.JvmField;


/**
 * 获取位置并返回
 */
public class GetPositionActivity extends AppActivity {
    private ActivityGetPositionBinding binding;

    Double currentLat;
    Double currentLong;
    String address = "";
    String companyName = "";

    private List<SupplierEntity3> mapSupplierList;

    private GetMapInfoPop getMapInfoPop;
    private PickMapAppWindow pickMapAppWindow;
    private boolean isShowBaidu;
    private boolean isShowGaode;
    private boolean isShowTenxun;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, GetPositionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.setClass(context, GetPositionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude, String address) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.putExtra("address", address);
        intent.setClass(context, GetPositionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude, String address, String companyName) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.putExtra("address", address);
        intent.putExtra("companyName", companyName);
        intent.setClass(context, GetPositionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initMap(savedInstanceState);
        initPoiSearch();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(GetPositionActivity.this, R.layout.activity_get_position);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(v -> finish());
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.titleView.setText("查看地图");
    }

    @Override
    protected void init() {
        currentLong = getIntent().getDoubleExtra("longitude", 0);
        currentLat = getIntent().getDoubleExtra("latitude", 0);
        address = getIntent().getStringExtra("address");
        companyName = getIntent().getStringExtra("companyName");

        getMapInfoPop = new GetMapInfoPop(GetPositionActivity.this);
        getMapInfoPop.setFocusable(false);
        getMapInfoPop.setOutsideTouchable(false);

    }

    private void initMap(Bundle savedInstanceState) {
        binding.mapView.initMap(savedInstanceState, getLifecycle(), new MapMarkerView.CallBack() {
            @Override
            public void onMarkerClick(int pos, String title) {
//                showMarkerView(pos, title);
            }

            @Override
            public void onLocation(Double Lat, Double Log, String string) {
                //详细地址
                address = string;
                if (!TextUtils.isEmpty(address)) {
//                    binding.include.titleView.setText(address);
                    getMapInfoPop.setStartData(address);
                }
            }

            @Override
            public void getAddress(Double Lat, Double Log, String address) {
                super.getAddress(Lat, Log, address);
            }

            @Override
            public void onRegeocodeSearched(Double Lat, Double Log, String string) {
                super.onRegeocodeSearched(Lat, Log, string);
                //详细地址
                address = string;
                if (!TextUtils.isEmpty(address)) {
//                    binding.include.titleView.setText(address);
                    getMapInfoPop.setStartData(address);
                }
            }
        });
    }

    private void initPoiSearch() {
        if (currentLat != 0 && currentLong != 0) {
            LatLng latLng = new LatLng(currentLat, currentLong);
            binding.mapView.setMyLocation(currentLat, currentLong, R.mipmap.parcker_map_position);
            binding.mapView.getLatLotExtra(currentLat, currentLong);
            binding.companyName.setText(companyName);
            binding.currentPosition.setText(address);
            binding.goButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showHintDialog(GetPositionActivity.this);
                }
            });
        }
    }

    protected void showHintDialog(Context context) {
        if (isAvilible(context, "com.baidu.BaiduMap")) {
            isShowBaidu = true;
        }
        if (isAvilible(context, "com.autonavi.minimap")) {
            isShowGaode = true;
        }
        if (isAvilible(context, "com.tencent.map")) {
            isShowTenxun = true;
        }
        if (!isShowBaidu && !isShowGaode && !isShowTenxun) {
            MToast.showLongToast1(context, "未检索到地图App", Gravity.CENTER);
            return;
        }
        showPopwindow(isShowBaidu, isShowGaode, isShowTenxun);
    }

    protected void showPopwindow(boolean showBaidu, boolean showGaode, boolean showTenxun) {
        pickMapAppWindow = new PickMapAppWindow(getActivity(), 3);
        pickMapAppWindow.setBgView(binding.grayLayout);
        pickMapAppWindow.showPopwindowBottom(binding.mapView);
        pickMapAppWindow.setData(currentLat, currentLong, address);
        pickMapAppWindow.setShowData(showBaidu, showGaode, showTenxun);
    }

    public void showMarkerView(int pos, String title) {
        getMapInfoPop.showPopwindowBottom(binding.mapView);
    }

}
